<?php
define("EW_PAGE_ID", "edit", TRUE); // Page ID
define("EW_TABLE_NAME", 'useronline', TRUE);
?>
<?php 
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg50.php" ?>
<?php include "ewmysql50.php" ?>
<?php include "phpfn50.php" ?>
<?php include "useronlineinfo.php" ?>
<?php include "userfn50.php" ?>
<?php include "usersinfo.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php

// Open connection to the database
$conn = ew_Connect();
?>
<?php
$Security = new cAdvancedSecurity();
?>
<?php
if (!$Security->IsLoggedIn()) $Security->AutoLogin();
$Security->LoadCurrentUserLevel('useronline');
if (!$Security->IsLoggedIn()) {
	$Security->SaveLastUrl();
	Page_Terminate("login.php");
}
if (!$Security->CanEdit()) {
	$Security->SaveLastUrl();
	Page_Terminate("useronlinelist.php");
}
?>
<?php

// Common page loading event (in userfn*.php)
Page_Loading();
?>
<?php

// Page load event, used in current page
Page_Load();
?>
<?php
$useronline->Export = @$_GET["export"]; // Get export parameter
$sExport = $useronline->Export; // Get export parameter, used in header
$sExportFile = $useronline->TableVar; // Get export file, used in header
?>
<?php

// Load key from QueryString
if (@$_GET["tgtmp"] <> "") {
	$useronline->tgtmp->setQueryStringValue($_GET["tgtmp"]);
}

// Create form object
$objForm = new cFormObj();
if (@$_POST["a_edit"] <> "") {
	$useronline->CurrentAction = $_POST["a_edit"]; // Get action code
	LoadFormValues(); // Get form values
} else {
	$useronline->CurrentAction = "I"; // Default action is display
}

// Check if valid key
if ($useronline->tgtmp->CurrentValue == "") Page_Terminate($useronline->getReturnUrl()); // Invalid key, exit
switch ($useronline->CurrentAction) {
	case "I": // Get a record to display
		if (!LoadRow()) { // Load Record based on key
			$_SESSION[EW_SESSION_MESSAGE] = "No records found"; // No record found
			Page_Terminate($useronline->getReturnUrl()); // Return to caller
		}
		break;
	Case "U": // Update
		$useronline->SendEmail = TRUE; // Send email on update success
		if (EditRow()) { // Update Record based on key
			$_SESSION[EW_SESSION_MESSAGE] = "Update successful"; // Update success
			Page_Terminate($useronline->getReturnUrl()); // Return to caller
		} else {
			RestoreFormValues(); // Restore form values if update failed
		}
}

// Render the record
$useronline->RowType = EW_ROWTYPE_EDIT; // Render as edit
RenderRow();
?>
<?php include "header.php" ?>
<script type="text/javascript">
<!--
var EW_PAGE_ID = "edit"; // Page id
var EW_SHOW_HIGHLIGHT = "Show highlight"; 
var EW_HIDE_HIGHLIGHT = "Hide highlight";

//-->
</script>
<script type="text/javascript">
<!--

function ew_ValidateForm(fobj) {
	if (fobj.a_confirm && fobj.a_confirm.value == "F")
		return true;
	var i, elm, aelm, infix;
	var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
	for (i=0; i<rowcnt; i++) {
		infix = (fobj.key_count) ? String(i+1) : "";
		elm = fobj.elements["x" + infix + "_tgtmp"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - tgtmp"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_tgtmp"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - tgtmp"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_date"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - date"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_date"];
		if (elm && !ew_CheckEuroDate(elm.value)) {
			if (!ew_OnError(elm, "Incorrect date, format = dd/mm/yyyy - date"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_ip"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - ip"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_local"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - local"))
				return false;
		}
	}
	return true;
}

//-->
</script>
<script type="text/javascript">
<!--
var ew_DHTMLEditors = [];

//-->
</script>
<link rel="stylesheet" type="text/css" media="all" href="calendar/calendar-win2k-1.css" title="win2k-1" />
<script type="text/javascript" src="calendar/calendar.js"></script>
<script type="text/javascript" src="calendar/lang/calendar-en.js"></script>
<script type="text/javascript" src="calendar/calendar-setup.js"></script>
<script type="text/javascript">
<!--
var ew_MultiPagePage = "Page"; // multi-page Page Text
var ew_MultiPageOf = "of"; // multi-page Of Text
var ew_MultiPagePrev = "Prev"; // multi-page Prev Text
var ew_MultiPageNext = "Next"; // multi-page Next Text

//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--

// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->

</script>
<p><span class="phpmaker">Edit TABLE: useronline<br><br><a href="<?php echo $useronline->getReturnUrl() ?>">Go Back</a></span></p>
<?php
if (@$_SESSION[EW_SESSION_MESSAGE] <> "") {
?>
<p><span class="ewmsg"><?php echo $_SESSION[EW_SESSION_MESSAGE] ?></span></p>
<?php
	$_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message
}
?>
<form name="fuseronlineedit" id="fuseronlineedit" action="useronlineedit.php" method="post" onSubmit="return ew_ValidateForm(this);">
<p>
<input type="hidden" name="a_edit" id="a_edit" value="U">
<table class="ewTable">
	<tr class="ewTableRow">
		<td class="ewTableHeader">tgtmp<span class='ewmsg'>&nbsp;*</span></td>
		<td<?php echo $useronline->tgtmp->CellAttributes() ?>><span id="cb_x_tgtmp">
<div<?php echo $useronline->tgtmp->ViewAttributes() ?>><?php echo $useronline->tgtmp->EditValue ?></div>
<input type="hidden" name="x_tgtmp" id="x_tgtmp" value="<?php echo ew_HtmlEncode($useronline->tgtmp->CurrentValue) ?>">
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">date<span class='ewmsg'>&nbsp;*</span></td>
		<td<?php echo $useronline->date->CellAttributes() ?>><span id="cb_x_date">
<input type="text" name="x_date" id="x_date"  value="<?php echo $useronline->date->EditValue ?>"<?php echo $useronline->date->EditAttributes() ?>>
&nbsp;<img src="images/ew_calendar.gif" id="cx_date" alt="Pick a Date" style="cursor:pointer;cursor:hand;">
<script type="text/javascript">
Calendar.setup(
{
inputField : "x_date", // ID of the input field
ifFormat : "%d/%m/%Y", // the date format
button : "cx_date" // ID of the button
}
);
</script>
</span></td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">ip<span class='ewmsg'>&nbsp;*</span></td>
		<td<?php echo $useronline->ip->CellAttributes() ?>><span id="cb_x_ip">
<input type="text" name="x_ip" id="x_ip"  size="30" maxlength="50" value="<?php echo $useronline->ip->EditValue ?>"<?php echo $useronline->ip->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">local<span class='ewmsg'>&nbsp;*</span></td>
		<td<?php echo $useronline->local->CellAttributes() ?>><span id="cb_x_local">
<input type="text" name="x_local" id="x_local"  size="30" maxlength="100" value="<?php echo $useronline->local->EditValue ?>"<?php echo $useronline->local->EditAttributes() ?>>
</span></td>
	</tr>
</table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="   Edit   ">
</form>
<script language="JavaScript" type="text/javascript">
<!--

// Write your table-specific startup script here
// document.write("page loaded");
//-->

</script>
<?php include "footer.php" ?>
<?php

// If control is passed here, simply terminate the page without redirect
Page_Terminate();

// -----------------------------------------------------------------
//  Subroutine Page_Terminate
//  - called when exit page
//  - clean up connection and objects
//  - if url specified, redirect to url, otherwise end response
function Page_Terminate($url = "") {
	global $conn;

	// Page unload event, used in current page
	Page_Unload();

	// Global page unloaded event (in userfn*.php)
	Page_Unloaded();

	 // Close Connection
	$conn->Close();

	// Go to url if specified
	if ($url <> "") {
		ob_end_clean();
		header("Location: $url");
	}
	exit();
}
?>
<?php

// Load form values
function LoadFormValues() {

	// Load from form
	global $objForm, $useronline;
	$useronline->tgtmp->setFormValue($objForm->GetValue("x_tgtmp"));
	$useronline->date->setFormValue($objForm->GetValue("x_date"));
	$useronline->date->CurrentValue = ew_UnFormatDateTime($useronline->date->CurrentValue, 7);
	$useronline->ip->setFormValue($objForm->GetValue("x_ip"));
	$useronline->local->setFormValue($objForm->GetValue("x_local"));
}

// Restore form values
function RestoreFormValues() {
	global $useronline;
	$useronline->tgtmp->CurrentValue = $useronline->tgtmp->FormValue;
	$useronline->date->CurrentValue = $useronline->date->FormValue;
	$useronline->date->CurrentValue = ew_UnFormatDateTime($useronline->date->CurrentValue, 7);
	$useronline->ip->CurrentValue = $useronline->ip->FormValue;
	$useronline->local->CurrentValue = $useronline->local->FormValue;
}
?>
<?php

// Load row based on key values
function LoadRow() {
	global $conn, $Security, $useronline;
	$sFilter = $useronline->SqlKeyFilter();
	if (!is_numeric($useronline->tgtmp->CurrentValue)) {
		return FALSE; // Invalid key, exit
	}
	$sFilter = str_replace("@tgtmp@", ew_AdjustSql($useronline->tgtmp->CurrentValue), $sFilter); // Replace key value

	// Call Row Selecting event
	$useronline->Row_Selecting($sFilter);

	// Load sql based on filter
	$useronline->CurrentFilter = $sFilter;
	$sSql = $useronline->SQL();
	if ($rs = $conn->Execute($sSql)) {
		if ($rs->EOF) {
			$LoadRow = FALSE;
		} else {
			$LoadRow = TRUE;
			$rs->MoveFirst();
			LoadRowValues($rs); // Load row values

			// Call Row Selected event
			$useronline->Row_Selected($rs);
		}
		$rs->Close();
	} else {
		$LoadRow = FALSE;
	}
	return $LoadRow;
}

// Load row values from recordset
function LoadRowValues(&$rs) {
	global $useronline;
	$useronline->tgtmp->setDbValue($rs->fields('tgtmp'));
	$useronline->date->setDbValue($rs->fields('date'));
	$useronline->ip->setDbValue($rs->fields('ip'));
	$useronline->local->setDbValue($rs->fields('local'));
}
?>
<?php

// Render row values based on field settings
function RenderRow() {
	global $conn, $Security, $useronline;

	// Call Row Rendering event
	$useronline->Row_Rendering();

	// Common render codes for all row types
	// tgtmp

	$useronline->tgtmp->CellCssStyle = "";
	$useronline->tgtmp->CellCssClass = "";

	// date
	$useronline->date->CellCssStyle = "";
	$useronline->date->CellCssClass = "";

	// ip
	$useronline->ip->CellCssStyle = "";
	$useronline->ip->CellCssClass = "";

	// local
	$useronline->local->CellCssStyle = "";
	$useronline->local->CellCssClass = "";
	if ($useronline->RowType == EW_ROWTYPE_VIEW) { // View row
	} elseif ($useronline->RowType == EW_ROWTYPE_ADD) { // Add row
	} elseif ($useronline->RowType == EW_ROWTYPE_EDIT) { // Edit row

		// tgtmp
		$useronline->tgtmp->EditCustomAttributes = "";
		$useronline->tgtmp->EditValue = $useronline->tgtmp->CurrentValue;
		$useronline->tgtmp->CssStyle = "";
		$useronline->tgtmp->CssClass = "";
		$useronline->tgtmp->ViewCustomAttributes = "";

		// date
		$useronline->date->EditCustomAttributes = "";
		$useronline->date->EditValue = ew_HtmlEncode(ew_FormatDateTime($useronline->date->CurrentValue, 7));

		// ip
		$useronline->ip->EditCustomAttributes = "";
		$useronline->ip->EditValue = ew_HtmlEncode($useronline->ip->CurrentValue);

		// local
		$useronline->local->EditCustomAttributes = "";
		$useronline->local->EditValue = ew_HtmlEncode($useronline->local->CurrentValue);
	} elseif ($useronline->RowType == EW_ROWTYPE_SEARCH) { // Search row
	}

	// Call Row Rendered event
	$useronline->Row_Rendered();
}
?>
<?php

// Update record based on key values
function EditRow() {
	global $conn, $Security, $useronline;
	$sFilter = $useronline->SqlKeyFilter();
	if (!is_numeric($useronline->tgtmp->CurrentValue)) {
		return FALSE;
	}
	$sFilter = str_replace("@tgtmp@", ew_AdjustSql($useronline->tgtmp->CurrentValue), $sFilter); // Replace key value
	$useronline->CurrentFilter = $sFilter;
	$sSql = $useronline->SQL();
	$conn->raiseErrorFn = 'ew_ErrorFn';
	$rs = $conn->Execute($sSql);
	$conn->raiseErrorFn = '';
	if ($rs === FALSE)
		return FALSE;
	if ($rs->EOF) {
		$EditRow = FALSE; // Update Failed
	} else {

		// Save old values
		$rsold =& $rs->fields;
		$rsnew = array();

		// Field tgtmp
		// Field date

		$useronline->date->SetDbValueDef(ew_UnFormatDateTime($useronline->date->CurrentValue, 7), ew_CurrentDate());
		$rsnew['date'] =& $useronline->date->DbValue;

		// Field ip
		$useronline->ip->SetDbValueDef($useronline->ip->CurrentValue, "");
		$rsnew['ip'] =& $useronline->ip->DbValue;

		// Field local
		$useronline->local->SetDbValueDef($useronline->local->CurrentValue, "");
		$rsnew['local'] =& $useronline->local->DbValue;

		// Call Row Updating event
		$bUpdateRow = $useronline->Row_Updating($rsold, $rsnew);
		if ($bUpdateRow) {
			$conn->raiseErrorFn = 'ew_ErrorFn';
			$EditRow = $conn->Execute($useronline->UpdateSQL($rsnew));
			$conn->raiseErrorFn = '';
		} else {
			if ($useronline->CancelMessage <> "") {
				$_SESSION[EW_SESSION_MESSAGE] = $useronline->CancelMessage;
				$useronline->CancelMessage = "";
			} else {
				$_SESSION[EW_SESSION_MESSAGE] = "Update cancelled";
			}
			$EditRow = FALSE;
		}
	}

	// Call Row Updated event
	if ($EditRow) {
		$useronline->Row_Updated($rsold, $rsnew);
	}
	$rs->Close();
	return $EditRow;
}
?>
<?php

// Page Load event
function Page_Load() {

	//echo "Page Load";
}

// Page Unload event
function Page_Unload() {

	//echo "Page Unload";
}
?>
